/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.Vector;

import Models.NhatKyHT_Model;

/**
 *
 * @author Venus
 */
public class NhatKyHT_Controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            int langid = (Integer) session.getAttribute("langid");
            String action = request.getParameter("action");
            if (action.equals("Show")) {
                String item = request.getParameter("item");
                NhatKyHT_Model kvm = new NhatKyHT_Model();
                Vector v = new Vector();
                v = kvm.LayDSNhatKyHT(langid);
                request.setAttribute("DSNK", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/nhatkyht.jsp");
                rd.forward(request, response);
            } else if (action.equals("SearchNhatKy")) {                
                String nhanvien = request.getParameter("nhanvien");
                String thaotac = request.getParameter("thaotac");
                String doituong = request.getParameter("doituong");
                String item = request.getParameter("item");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                NhatKyHT_Model nkm = new NhatKyHT_Model();
                Vector v = new Vector();
                v = nkm.SearchNhatKy( tungay , tugio, tuphut , denngay , dengio , denphut , nhanvien, thaotac, doituong, langid);                
                request.setAttribute("nhanvien", nhanvien);
                request.setAttribute("thaotac", thaotac);
                request.setAttribute("doituong", doituong);
                request.setAttribute("item", item);
                request.setAttribute("DSNK", v);
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssearchhistory.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortNgayGio")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhatKyHT_Model nkm = new NhatKyHT_Model();
                Vector v = new Vector();
                v = nkm.SortNgayGio(langid, order);
                request.setAttribute("DSNK", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssorthistory.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortNhanVien")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhatKyHT_Model nkm = new NhatKyHT_Model();
                Vector v = new Vector();
                v = nkm.SortNhanVien(langid, order);
                request.setAttribute("DSNK", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssorthistory.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortThaoTacNK")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhatKyHT_Model nkm = new NhatKyHT_Model();
                Vector v = new Vector();
                v = nkm.SortThaoTacNK(langid, order);
                request.setAttribute("DSNK", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssorthistory.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortDoiTuongNK")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhatKyHT_Model nkm = new NhatKyHT_Model();
                Vector v = new Vector();
                v = nkm.SortDoiTuongNK(langid, order);
                request.setAttribute("DSNK", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "4");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssorthistory.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchNgayGioNK")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                String nhanvien = request.getParameter("nhanvien");
                String doituong = request.getParameter("doituong");
                String thaotac = request.getParameter("thaotac");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhatKyHT_Model nkm = new NhatKyHT_Model();
                Vector v = new Vector();
                v = nkm.SortSearchNgayGioNK(langid, order, tungay , tugio, tuphut , denngay , dengio , denphut , nhanvien, thaotac, doituong);
                request.setAttribute("DSNK", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                request.setAttribute("nhanvien", nhanvien);
                request.setAttribute("thaotac", thaotac);
                request.setAttribute("doituong", doituong);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchhistory.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchNhanVienNK")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                String nhanvien = request.getParameter("nhanvien");
                String doituong = request.getParameter("doituong");
                String thaotac = request.getParameter("thaotac");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhatKyHT_Model nkm = new NhatKyHT_Model();
                Vector v = new Vector();
                v = nkm.SortSearchNhanVienNK(langid, order, tungay , tugio, tuphut , denngay , dengio , denphut , nhanvien, thaotac, doituong);
                request.setAttribute("DSNK", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                request.setAttribute("nhanvien", nhanvien);
                request.setAttribute("thaotac", thaotac);
                request.setAttribute("doituong", doituong);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchhistory.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchThaoTacNK")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                String nhanvien = request.getParameter("nhanvien");
                String doituong = request.getParameter("doituong");
                String thaotac = request.getParameter("thaotac");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhatKyHT_Model nkm = new NhatKyHT_Model();
                Vector v = new Vector();
                v = nkm.SortSearchThaoTacNK(langid, order, tungay , tugio, tuphut , denngay , dengio , denphut , nhanvien, thaotac, doituong);
                request.setAttribute("DSNK", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                request.setAttribute("nhanvien", nhanvien);
                request.setAttribute("thaotac", thaotac);
                request.setAttribute("doituong", doituong);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchhistory.jsp");
                rd.forward(request, response);
            } else if (action.equals("SortSearchDoiTuongNK")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                String nhanvien = request.getParameter("nhanvien");
                String doituong = request.getParameter("doituong");
                String thaotac = request.getParameter("thaotac");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                NhatKyHT_Model nkm = new NhatKyHT_Model();
                Vector v = new Vector();
                v = nkm.SortSearchDoiTuongNK(langid, order, tungay , tugio, tuphut , denngay , dengio , denphut , nhanvien, thaotac, doituong);
                request.setAttribute("DSNK", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "4");
                request.setAttribute("tungay", tungay);
                request.setAttribute("tugio", tugio);
                request.setAttribute("tuphut", tuphut);
                request.setAttribute("denngay", denngay);
                request.setAttribute("dengio", dengio);
                request.setAttribute("denphut", denphut);
                request.setAttribute("nhanvien", nhanvien);
                request.setAttribute("thaotac", thaotac);
                request.setAttribute("doituong", doituong);
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchhistory.jsp");
                rd.forward(request, response);
            } else if (action.equals("DeleteKV")) {
                String makv = request.getParameter("makv");
                String item = request.getParameter("item");
                String MaKhuVuc[] = makv.split("-");
                int j = 0;
                while (j < MaKhuVuc.length) {
                    NhatKyHT_Model.DeleteKV(MaKhuVuc[j]);
                    j = j + 1;
                }
                NhatKyHT_Model nkm = new NhatKyHT_Model();
                Vector v = new Vector();
                v = nkm.LayDSNhatKyHT(langid);
                request.setAttribute("item", item);
                request.setAttribute("DSNK", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rsdeletehistory.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("CheckDate")) {
                String tungay = request.getParameter("tungay");
                String tugio = request.getParameter("tugio");
                String tuphut = request.getParameter("tuphut");
                String denngay = request.getParameter("denngay");
                String dengio = request.getParameter("dengio");
                String denphut = request.getParameter("denphut");
                if(tungay.equals("") && denngay.equals("")){
                    out.print("true");
                }
                else{
                    if(NhatKyHT_Model.CheckDate(tungay, tugio, tuphut, denngay, dengio, denphut)){
                        out.print("true");
                    }
                    else{
                        out.print("false");
                    }
                }
            }
        } finally {
            out.close();
        }
    }    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
